package com.showapi.app.util;


import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class CalendarHelper {
    public static String getCurrentTime() {
        SimpleDateFormat dateformat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");
        return dateformat.format(new Date());

    }

    public static String getCurrentTimeDetail() {
        SimpleDateFormat dateformat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss.SSS");
        return dateformat.format(new Date());
    }

    // 格式化数字,带两位小数
    public static double formatNum(double num) {
        DecimalFormat df = new DecimalFormat("#.##");
        return Double.parseDouble(df.format(num));
    }

    // 用自定义格式来格式化数字
    public static String formatNumWithStyle(double num, String style) {
        DecimalFormat df = new DecimalFormat(style);
        return df.format(num);
    }


    // 得到一天的两个边界
    public static Map getWholeDay(Date date) {
        Map map = new HashMap();
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        ca.set(Calendar.HOUR_OF_DAY, 0);
        ca.set(Calendar.MINUTE, 0);
        ca.set(Calendar.SECOND, 0);
        ca.set(Calendar.MILLISECOND, 0);
        map.put("begin", ca.getTime());
        Calendar ca2 = (Calendar) ca.clone();
        ca2.set(Calendar.DATE, ca2.get(Calendar.DATE) + 1);
        map.put("end", ca2.getTime());
        return map;
    }

    // 得到一个月的两个边界
    public static Map getWholeMonth(Date date) {
        Map map = new HashMap();
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        ca.set(Calendar.DAY_OF_MONTH, 1);
        ca.set(Calendar.HOUR_OF_DAY, 0);
        ca.set(Calendar.MINUTE, 0);
        ca.set(Calendar.SECOND, 0);
        ca.set(Calendar.MILLISECOND, 0);
        map.put("begin", ca.getTime());
        Calendar ca2 = (Calendar) ca.clone();
        ca2.add(Calendar.MONTH, 1);
        map.put("end", ca2.getTime());
        return map;

    }

    public static Date getTimeFromStr(Object str) {
        Date date = null;
        if (str == null)
            return null;
        if (str.toString().trim().length() == 0)
            return null;
        if (str.toString().trim().indexOf(" ") == -1)
            str = str + " 00:00:00";
        if (str.toString().length() < 17)
            str = str + ":00";
        SimpleDateFormat sdf = new SimpleDateFormat(
                "yyyy-MM-dd hh:mm:ss");
        try {
            date = sdf.parse(str.toString());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static Date getTimeFromStr(String str) {
        if (str == null)
            return null;
        if (str.toString().trim().length() == 0)
            return null;
        if (str.toString().trim().indexOf(" ") == -1)
            str = str + " 00:00:00";
        if (str.toString().length() < 17)
            str = str + ":00";
        SimpleDateFormat sdf = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = sdf.parse(str.toString());
        } catch (ParseException e) {
            date = new Date();
            e.printStackTrace();
        }
        return date;
    }

    public static String getStringFromDate(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return getTime(cal);
    }

    /**
     *
     * @return
     * @throws ParseException
     */
    public static Date getTimeFromStr(String str, String patern) {
        if (str == null)
            return null;
        SimpleDateFormat sdf = new SimpleDateFormat(patern);
        Date date = null;
        try {
            date = sdf.parse(str);
        } catch (ParseException e) {
            date = new Date();
            e.printStackTrace();
        }
        return date;
    }

    public static Date getTimeFromCalendar(Calendar cal, String patern) {
        if (cal == null)
            return null;
        SimpleDateFormat sdf = new SimpleDateFormat(patern);
        Date date = null;
        try {
            date = sdf.parse(getTime(cal));
        } catch (ParseException e) {
            date = new Date();
            e.printStackTrace();
        }
        return date;
    }

    // 得到20070719
    public static int getIntDayFromString(String str) {
        String d = str.substring(0, 10).replaceAll("-", "");
        return Integer.parseInt(d);
    }

    public static void main(def aas) throws Exception {

    }
}
